﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpDX;

namespace BoatNGoat.Logic
{
    class RandomWanderBehaviour : IMobBehaviour
    {
        private Vector2 _lastVector = Vector2.Zero;

        private double _seconds = 0;
        private double _nextDecisionTime;

        public RandomWanderBehaviour()
        {
            UpdateNextDecisionTime();
        }
        
        public Vector2 MakeDecision(Server.ServerTime time, SharpDX.Vector2 latestPosition)
        {
            _seconds += time.ElapsedGameTime.TotalSeconds;

            if (!TimeForNewDecision())
            {
                return _lastVector;
            }

            double stop = Rnd.NextDouble();
            if (stop > 0.10)
            {
                _lastVector = Vector2.Zero;
            }
            else
            {
                float angle = (float)(Math.PI * 2 * Rnd.NextDouble());
                Matrix rot = Matrix.RotationZ(angle);
                Vector2 startVec = new Vector2(0, (float)(Rnd.NextDouble() * 0.2 + 0.9));
                Vector2.TransformCoordinate(ref startVec, ref rot, out _lastVector);
            }

            UpdateNextDecisionTime();
            return _lastVector;
        }

        private bool TimeForNewDecision()
        {
            return _seconds >= _nextDecisionTime;
        }

        private void UpdateNextDecisionTime()
        {
            _seconds = 0.0;
            _nextDecisionTime = 2 + (Rnd.NextDouble() * 3);
        }
    }
}
